import {
    axios
} from '../../../utils/ajax.js';

Page({
    data: {
        searched: false,
        keyword: '',
        showDelete: false,
        totopshow: false
    },

    onLoad: function(options) {
        // console.log(options)
        this.setData({
            goodstype: options.isVip == true ? 0 : 1
        })
        axios({
            url: '/goods/hotSearch',
            data: {
                userid: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').userid : null,
                token: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').token : null
            }
        }).then(res => {
            // console.log(res.data)
            this.setData({
                keywords: res.data.keywords
            })
        })

        axios({
            url: '/goods/guessLike',
            data: {
                userid: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').userid : null,
                token: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').token : null
            }
        }).then(res => {
            // console.log(res.data)
            this.setData({
                likelist: res.data.goodslist
            })
        })
    },

    confirm: function(e) {
        // console.log(e.detail.value)
        let value = e.detail.value;
        if (value != '') {
            getGoodsList(value, 1, this)
        }
    },

    tapHot: function(e) {
        let keyword = e.currentTarget.dataset.keyword;
        this.setData({
            keyword: keyword,
            showDelete: keyword ? true : false
        })
        getGoodsList(keyword, 1, this)
    },

    onReachBottom: function() {
        if (this.data.page > 0) {
            getGoodsList(this.data.keyword, ++this.data.page, this)
        }
    },

    deleteInput: function() {
        this.setData({
            searched: false,
            keyword: '',
            showDelete: false,
            goodslist: []
        })
    },

    changeInput: function(e) {
        this.setData({
            keyword: e.detail.value,
            showDelete: e.detail.value ? true : false
        })
    },

    onPageScroll: function(e) {
        this.setData({
            totopshow: e.scrollTop > 400 ? true : false
        })
    }
})

function getGoodsList(keyword, page, that) {
    axios({
        url: '/goods/search',
        data: {
            keyword: keyword,
            page: page,
            userid: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').userid : null,
            token: wx.getStorageSync('userinfo') ? wx.getStorageSync('userinfo').token : null
        }
    }).then((res) => {
        // console.log(res.data)
        that.setData({
            searched: true,
            keyword: keyword,
            goodslist: page > 1 ? that.data.goodslist.concat(res.data.goodsList) : res.data.goodsList,
            page: res.data.goodsList && res.data.goodsList.length > 0 ? page : 0,
            // totalpages: res.data.totalpages,
            canload: res.data.goodsList && res.data.goodsList.length == 30 ? true : false
        })
    })
}